//获取应用实例
const http = require('../../../utils/httpUtils.js')
const app = getApp();
import {
    cityData
} from '../../../utils/city.js'
Page({
    data: {
        navbarData: {
            showCapsule: 1, //是否显示左上角图标
            title: '添加地址' //导航栏 中间的标题
        },
        height: app.globalData.height * 2 + 20,
        prompt: '请选择',
        defAddress: ['河南省', '郑州市', '金水区'],
        addresId: []
    },
    onLoad(e) {
        console.log(e.data)
        if (e.data != undefined) {
            let data = JSON.parse(e.data)
            this.setData({
                upData: data,
                prompt: data.uc_province + '&ensp;' + data.uc_city + '&ensp;' + data.uc_county
            })
            this.data.addresId[0] = data.uc_province_id
            this.data.addresId[1] = data.uc_cityId
            this.data.addresId[2] = data.uc_county_id
        }
    },
    bindCancel() {
        wx.navigateBack({})
    },
    //保存修改
    bindSave(e) {
        let data = e.detail.value
        if (data.uc_consignee == "") {
            wx.showModal({
                title: '提示',
                content: '请填写联系人姓名',
                showCancel: false
            })
            return
        }
        if (isNaN(data.uc_phone) || data.uc_phone.length != 11) {
            wx.showModal({
                title: '提示',
                content: '手机号码格式不对',
                showCancel: false
            })
            return
        }

        if (this.data.prompt == "请选择") {
            wx.showModal({
                title: '提示',
                content: '请选择地区',
                showCancel: false
            })
            return
        }

        // if (data.street == "") {
        //   wx.showModal({
        //     title: '提示',
        //     content: '请填写街道地址',
        //     showCancel: false
        //   })
        //   return
        // }
        if (data.location == "") {
            wx.showModal({
                title: '提示',
                content: '请填写详细地址',
                showCancel: false
            })
            return
        }
        data.uc_province_id = this.data.addresId[0]
        data.uc_city_id = this.data.addresId[1]
        data.uc_county_id = this.data.addresId[2]
        data.c_client = "weixinSmall";
        data.uc_city = this.data.citys
        data.uc_county = this.data.county;
        data.uc_province = this.data.province;
        this.submit(data)
    },
    submit(data) {
        if (this.data.upData != undefined) {
            data.uc_id = this.data.upData.uc_id
            http._headerPost('/api/userConsign/add_user_consign', data, res => {
                if (res.data.code == 1) {
                    wx.showModal({
                        title: '提示',
                        content: '修改成功',
                        showCancel: false,
                        success(res) {
                            wx.navigateBack({})
                        }
                    })
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    })
                }
            })
        } else {
            data.mold = 'user'
            http._headerPost('/api/userConsign/add_user_consign', data, res => {
                if (res.data.code == 1) {
                    wx.showModal({
                        title: '提示',
                        content: '添加成功',
                        showCancel: false,
                        success(res) {
                            wx.navigateBack({})
                        }
                    })
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    })
                }
            })
        }
    },
    bindRegionChange(e) {
        this.setData({
            province: e.detail.value[0],
            citys: e.detail.value[1],
            county: e.detail.value[2],
            prompt: e.detail.value[0] + '&ensp;' + e.detail.value[1] + '&ensp;' + e.detail.value[2]
        })
        this.addressToCode()
    },
    //查询地址ID
    addressToCode() {
        cityData.forEach(province => {
            if (province.name == this.data.province) {
                this.data.addresId[0] = province.id
                province.cityList.forEach(citys => {
                    if (citys.name == this.data.citys) {
                        this.data.addresId[1] = citys.id
                        citys.districtList.forEach(county => {
                            if (county.name == this.data.county) {
                                this.data.addresId[2] = county.id
                            }
                        })
                    }
                })
            }
        })
    }
})